<!doctype html>
<html lang="zh-Hans-CN">

<head>
    <meta charset="utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0, viewport-fit=cover">
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <title>支付结果</title>
    <link rel="stylesheet" href="/ui/mobile/iconfont/iconfont.css">
    <link rel="stylesheet" href="/ui/mobile/css/ui.all.css"/>
    <link rel="stylesheet" href="/ui/mobile/css/style.css" type="text/css"/>
</head>

<body>
<main class="c-main  bg-ban">
    <div data-wrap="orderResult"></div>
    <div data-wrap="orderInfo"></div>
</main>
<script type="text/javascript" src="/m/public/all.ui.js"></script>
<script src="/ui/mobile/iconfont/iconfont.js"></script>
<script src="/m/public/util.js"></script>

<script src="/m/questionOrder/js/getOrderInfoWidget.js"></script>

<script type="text/javascript">
    const {orderId} = $.getPageParam();
    let loadIndex = 0;

    if (!orderId) {
        location.href = "/m/error.html?" + $.param({msg: "订单ID不能为空", back: window.location.href});
    }

    const loading = $.loading("加载中");
    loadResult(data => {
        loading.remove();
        $('[data-wrap="orderResult"]').replaceWith(getOrderResultWidget(data));
        $('[data-wrap="orderInfo"]').replaceWith(getOrderInfoWidget(data));
    });


    function getOrderResultWidget({orderState, message}) {
        const $dom = $(`<div></div>`);
        if (orderState === 2 || orderState === 3) {
            $dom.append(`<h3 class="success-tit"><i class="iconfont icon-chenggong"></i>支付成功</h3>`);
        } else {
            $dom.append(`<h3 class="success-tit"><i class="iconfont icon-jiaoyiquxiao"></i>${message ? message : "支付失败"}</h3>`);
        }

        $dom.append(`<a class="l-detail" href="${"/m/questionOrder/myOrder.html?" + $.param({orderId})}">查看预约记录<i class="iconfont icon-xiangyou"></i></a>`);

        return $dom;


    }

    function loadResult(callback = Function()) {
        getQuesOrderDetails({orderId}, ({Code, Message, Result: data}) => {
            if (Code !== 10000) {
                data.orderState = Code;
                data.message = Message;
                return callback(data);
            }

            if (data.orderState !== 1) {
                //得到支付结果
                return callback(data);
            }

            if (loadIndex > 10) {
                //等待超时
                data.orderState = -10000;
                data.message = "支付等待超时";
                return callback(data);
            }

            setTimeout(() => {
                loadIndex += 1;
                loadResult(callback);
            }, loadIndex <= 5 ? 5000 : 10000);
        });

    }

    function getQuesOrderDetails(param, callback = Function()) {
        return $.sApi({
            data: param,
            showLoading: false,
            url: "/api/QuestionOrder/getQuesOrderDetails"
        }, callback)
    }

</script>

</body>
</html>